package com.market.dao.base;

import com.market.entity.BaseEntity;
import com.market.utils.CoreUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BaseDaoImpl<T extends BaseEntity> extends SqlSessionSupport implements IBaseDao<T> {
    public static final String SQL_INSERT = "insert";
    public static final String SQL_BATCH_INSERT = "insertBatch";
    public static final String SQL_UPDATE = "update";
    public static final String SQL_BATCH_UPDATE = "updateBatch";
    public static final String SQL_GET_BY_ID = "getByPrimaryKey";
    public static final String SQL_DELETE_BY_ID = "deleteByPrimaryKey";
    public static final String SQL_DELETE = "delete";
    public static final String SQL_LOGIC_DELETE = "logicalDeletion"; //逻辑删除
    public static final String SQL_LIST_PAGE = "listPage";
    public static final String SQL_LIST_PAGE_COUNT = "listPageCount";
    public static final String SQL_LIST_BY = "getList";
    public static final String SQL_COUNT_BY_PAGE_PARAM = "countByPageParam"; // 根据当前分页参数进行统计


    /**
     * 查询
     * @param entity
     * @return
     * @throws Exception
     */
    public List<T> getList(T entity)throws Exception{
        return super.getSqlSession().selectList(getStatement(SQL_LIST_BY),entity);
    }

    /**
     * 查詢
     * @param entity
     * @param param
     * @return
     * @throws Exception
     */
    public List<T> getList(T entity,Map<String,Object> param)throws Exception{
        Map paramMap= CoreUtils.convertToMap(entity);
        if(param!=null){
            Iterator entries = param.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry entry = (Map.Entry) entries.next();
                String key = String.valueOf(entry.getKey());
                Object value = (Object)entry.getValue();
                if(paramMap.containsKey(key)){
                    paramMap.remove(key);
                }
                paramMap.put(key,value);
            }
        }
        return super.getSqlSession().selectList(getStatement(SQL_LIST_BY),paramMap);
    }

    /**
     * 查询（根据主键）
     * @param primaryKey
     * @return
     * @throws Exception
     */
    public T getByPrimaryKey(Object primaryKey) throws Exception {
        return super.getSqlSession().selectOne(getStatement(SQL_GET_BY_ID), primaryKey);
    }

    public boolean add(T entity) throws Exception {
        int result = super.getSqlSession().insert(getStatement(SQL_INSERT), entity);
        if (result <= 0) {

        }
        return result>0?true:false;
    }

    public int insert(List<T> list) throws Exception {
        if (list == null || list.size() <= 0) {
            return 0;
        }

        int result = super.getSqlSession().insert(getStatement(SQL_BATCH_INSERT), list);
        if (result <= 0) {

        }
        return result;
    }

    public int update(T entity) throws Exception {
        int result = super.getSqlSession().update(getStatement(SQL_UPDATE), entity);
        if (result <= 0) {

        }
        return result;
    }

    public int update(List<T> list) throws Exception {
        if (list == null || list.size() <= 0) {
            return 0;
        }
        int result = super.getSqlSession().update(getStatement(SQL_BATCH_UPDATE), list);
        if (result <= 0) {

        }
        return result;
    }

    public int deleteByPrimaryKey(Object primaryKey) throws Exception {
        return super.getSqlSession().delete(getStatement(SQL_DELETE_BY_ID), primaryKey);
    }

    public int delete(String[] ids)throws Exception{
        Map map=new HashMap();
        map.put("list",ids);
        return super.getSqlSession().delete(getStatement(SQL_DELETE), map);
    }

    public Long getCountByStatus(Map map)throws  Exception{
        Long totalNum=super.getSqlSession().selectOne(getStatement(SQL_LIST_PAGE_COUNT),map);
        return totalNum;
    }

    /**
     * 获取Mapper命名空间.
     *
     * @param sqlId
     * @return
     */
    public String getStatement(String sqlId) {
        String name = this.getClass().getName();
        StringBuffer sb = new StringBuffer();
        sb.append(name).append(".").append(sqlId);
        String statement = sb.toString();
        return statement;
    }

    public String getStatement(Class obj,String sqlId){
        String name = obj.getName();
        StringBuffer sb = new StringBuffer();
        sb.append(name).append(".").append(sqlId);
        String statement = sb.toString();
        return statement;
    }
}
